package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author: 青肆
 * @date: 2022/8/24 10:23
 * @description: ShoppingCartController
 */
@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    //新增

    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        log.info("购物车：{}", shoppingCart);

        //设置用户id，指定当前是哪个用户的购物车数据

        //获得当前用户id
        Long currentId = BaseContext.getCurrentId();
        //将id放到购物车中
        shoppingCart.setUserId(currentId);


        //购物车有可能是菜品信息dish_id,或者套餐信息setmeal_id
        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, currentId);

        if (dishId != null) {//说明添加的是菜品
            //将dishId放入购物车中
            queryWrapper.eq(ShoppingCart::getDishId, dishId);

        } else {//说明添加的是套餐
            //将getSetmealId放入购物车中
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        //查询当前菜品/套餐是否在购物车中
        //sql:seltect * from shoping_cart where id =? and dish_id/setmeal_id  =?
        //由于id是唯一的，只会有一条数据，所以使用getone
        ShoppingCart carServiceOne = shoppingCartService.getOne(queryWrapper);

        if (carServiceOne != null) {
            //如果已经存在，就在原来的基础上加一
            Integer number = carServiceOne.getNumber();
            carServiceOne.setNumber(number + 1);
            shoppingCartService.updateById(carServiceOne);
        } else {
            //如果不存在，则添加到购物车种，数量默认是一
            shoppingCart.setNumber(1);
            //添加数据
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            carServiceOne = shoppingCart;
        }
        return R.success(carServiceOne);
    }

    //查看

    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        log.info("查看购物车...");

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);

        return R.success(list);
    }

    @DeleteMapping("/clean")
    public R<String> clean() {

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());

        shoppingCartService.remove(queryWrapper);

        return R.success("清空成功！");
    }
}